package com.demo.utils;

import java.io.UnsupportedEncodingException;

public class CappCommonUtil {

    /**
     * @Name: toBrowserCode
     * @Description: 把中文字符转换为带百分号的浏览器编码
     * @param word 中文字符
     * @param encoding 字符编码
     */
    public static String toBrowserCode(String word, String encoding) {
        byte[] textByte = new byte[0];
        try {
            textByte = word.getBytes(encoding);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        StringBuilder strBuilder = new StringBuilder();
        for (int j = 0; j < textByte.length; j++) {
            // 转换为16进制字符
            String hexStr = Integer.toHexString(textByte[j] & 0xff);
            strBuilder.append("%" + hexStr.toUpperCase());
        }

        return strBuilder.toString();
    }
}
